package se.mobstrat.android.asobaudiobook;

import se.mobstrat.android.asobaudiobook.javascript.JavaScriptUtils;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class Main extends Activity {

	final static String	TAG	= "Main";

	private WebView		webView;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		webView = (WebView) findViewById(R.id.webview);
		webView.setWebChromeClient(new WebChromeClient());
		webView.setWebViewClient(new WebViewClient());
		webView.addJavascriptInterface(new JavaScriptUtils(this), "NativeInterface");
		webView.loadUrl("file:///android_asset/medialist.html");

		// Settings
		WebSettings webSettings = webView.getSettings();

		// Important
		webSettings.setJavaScriptEnabled(true);
		webSettings.setDomStorageEnabled(true);
		webSettings.setDatabaseEnabled(true);
		webSettings.setDatabasePath("/data/data/se.mobstrat.android.asobaudiobook/databases");
		webSettings.setAllowFileAccess(true);

		// Misc
		webSettings.setSavePassword(false);
		webSettings.setSaveFormData(false);
		webSettings.setSupportZoom(true);
	}

	public void clearCache() {
		log("Clear cache");
		webView.clearCache(true);
	}
	
	private void log(String msg) {
		Log.d(TAG, String.format("%s -- %s", TAG, msg));
	}
}